﻿using CaterBll;
using CaterModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CaterUI
{
    public partial class FormDishInfo : Form
    {
        public FormDishInfo()
        {
            InitializeComponent();
        }

        private DishInfoBll diBll = new DishInfoBll();
        private DishTypeInfoBll dtiBll = new DishTypeInfoBll();
        private int rowIndex = 0;

        private void FormDishInfo_Load(object sender, EventArgs e)
        {
            LoadTypeList();
            LoadList();
        }

        private void LoadList()
        {
            try
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();

                if (txtDtitle.Text != "")
                {
                    dic.Add("dtitle", txtDtitle.Text);
                }
                if (cmbDTypeId.SelectedValue.ToString() != "0")
                {
                    dic.Add("DTypeId", cmbDTypeId.SelectedValue.ToString());
                }

                dgvDishInfo.AutoGenerateColumns = false;
                dgvDishInfo.DataSource = diBll.GetList(dic);

                //设置选中行
                if (rowIndex > 0&& dgvDishInfo.Rows.Count>0)
                {
                    if (rowIndex > dgvDishInfo.Rows.Count - 1)
                    {
                        dgvDishInfo.Rows[0].Selected = true;
                    }
                    else
                    {
                        dgvDishInfo.Rows[rowIndex].Selected = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "出错了");
            }
        }

        //加载分类下拉框
        private void LoadTypeList()
        {
            try
            {
                List<DishTypeInfo> list = dtiBll.GetList();
                //插入全部
                list.Insert(0, new DishTypeInfo()
                {
                    DId = 0,
                    DTitle = "全部"
                });
                //查询时的下拉框类型
                cmbDTypeId.DataSource = list;
                cmbDTypeId.DisplayMember = "dtitle";
                cmbDTypeId.ValueMember = "did";

                cmbDishTypeAdd.DataSource = dtiBll.GetList();
                cmbDishTypeAdd.DisplayMember = "dtitle";
                cmbDishTypeAdd.ValueMember = "did";

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "出错了");
            }
        }



        private void textBox1_MouseLeave(object sender, EventArgs e)
        {
            LoadList();
        }

        /// <summary>
        /// 显示全部
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            rowIndex = 0;
            txtDtitle.Text = "";
            cmbDTypeId.SelectedIndex = 0;
            LoadList();
        }

        private void cmbDTypeId_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadList();
        }

        private void txtDtitleAdd_Leave(object sender, EventArgs e)
        {
            try
            {

                txtChar.Text = CaterCommon.PinYinHelper.GetPinyin(txtDtitleAdd.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "出错了");
            }
        }

        private void CleanData()
        {
            txtDid.Text = "添加时无编号";
            txtDtitleAdd.Text = "";
            txtPrice.Text = "";
            txtChar.Text = "";
            LoadTypeList();
        }

        //添加
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtDtitleAdd.Text == "")
                {
                    MessageBox.Show("请填写名称");
                    txtDtitleAdd.Focus();
                    return;
                }
                if (cmbDishTypeAdd.SelectedValue==null)
                {
                    MessageBox.Show("请选择分类");
                    cmbDishTypeAdd.Focus();
                    return;
                }
                if (txtPrice.Text == "")
                {
                    MessageBox.Show("请填写价格");
                    txtPrice.Focus();
                    return;
                }

                DishInfo di = new DishInfo();
                di.DTitle = txtDtitleAdd.Text.Trim();
                di.DTypeId =Convert.ToInt32(cmbDishTypeAdd.SelectedValue.ToString());
                di.DPrice = Convert.ToDecimal(txtPrice.Text.Trim());
                di.DChar = txtChar.Text;

                if(txtDid.Text=="添加时无编号")
                {
                    if(diBll.Insert(di)>0)
                    {
                        MessageBox.Show("添加成功");
                    }
                    else
                    {
                        MessageBox.Show("添加失败");
                    }
                }
                else
                {
                    di.DId = Convert.ToInt32(txtDid.Text);
                    if (diBll.Update(di) > 0)
                    {
                        MessageBox.Show("修改成功");
                    }
                    else
                    {
                        MessageBox.Show("修改失败");
                    }
                }

                CleanData();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "出错了");
            }
        }

        private void dgvDishInfo_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            rowIndex = e.RowIndex;
            var row = dgvDishInfo.Rows[e.RowIndex];
            txtDid.Text = row.Cells[0].Value.ToString();
            txtDtitleAdd.Text = row.Cells[1].Value.ToString();
            cmbDishTypeAdd.Text = row.Cells[2].Value.ToString();
            txtPrice.Text = row.Cells[3].Value.ToString();
            txtChar.Text = row.Cells[4].Value.ToString();
            btnSave.Text = "修改";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            CleanData();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult result = MessageBox.Show("确定要删除吗？", "删除提示", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    int id = Convert.ToInt32(dgvDishInfo.SelectedRows[0].Cells[0].Value);
                    if(diBll.Delete(id)>0)
                    {
                        LoadList();
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "出错了");
            }
        }

        private void btnAddType_Click(object sender, EventArgs e)
        {
            FormDishTypeInfo formDti = new FormDishTypeInfo();
            DialogResult result = formDti.ShowDialog();
            if(result==DialogResult.OK)
            {
                LoadTypeList();
                LoadList();
            }
        }
    }
}
